const Controller = require('egg').Controller;

class HomeController extends Controller {


  async index() {
    let app = '';
    app = this.ctx.service.sayhi.index();
    this.ctx.session.user = 'test';
    this.ctx.body = app;
  }

  // 请求java接口示例
  async client() {
    // 这里需要优化一下，至少要加一个全局变量，不然每次都要输入全部的url
    const result = await this.app.curl('https://registry.npm.taobao.org/egg/latest', {
      dataType: 'json',
    });
    this.ctx.logger.info('req query', this.ctx.request.query);
    this.ctx.session.user = 'tecllllst';
    this.ctx.body = result;
  }

  // post example
  async postEm() {
    this.ctx.logger.info('test', this.ctx.request.body);
    this.ctx.body = `body: ${JSON.stringify(this.ctx.request.body)}`;
  }

  // mongo example
  async mongo() {
    // this.ctx.logger.info(this.ctx.model.User.find);
    this.ctx.body = await this.ctx.model.User.find({});
  }

  async insertMongo() {
    const user = new this.ctx.model.User({ passwd: '123', user: 'test' });
    await user.save();
    this.ctx.body = user;
  }
}
// 映射出控制器
module.exports = HomeController;
